1768C - Elemental Decompress - CodeForces Solution


constructive algorithms greedy implementation sortings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve() 
{
   ll n; cin>>n;
   vector<ll> v(n),a(n,0),b(n,0),c(n,0),r(n,0);
   set<ll> s1,s2;
   for(ll i=0;i<n;i++)
    {
        cin>>v[i];
        if(v[i]>n)
        {
            cout<<"NO"<<endl;
            return ;
        }
        r[v[i]-1]++;
    }
    for(ll i=0;i<n;i++)
    {
        if(r[i]>2)
        {
            cout<<"NO"<<endl;
            return ;
        }
    }
   for(ll i=1;i<=n;i++)
   {
       s1.insert(i);
       s2.insert(i);
   }
   for(ll i=0;i<n;i++)
   {
       if(c[v[i]-1]==0)
       {
           a[i]=v[i];
           c[v[i]-1]=1;
           s1.erase(v[i]);
       }
       else
       {
           b[i]=v[i];
           s2.erase(v[i]);
       }
   }
   for(ll i=0;i<n;i++)
   {
       if(a[i]==0)
       {
           auto it=s1.upper_bound(b[i]);
           it--;
           if((*it)<=b[i])
           {
               a[i]=*it;
               s1.erase(a[i]);
           }
           else
           {
               cout<<"NO"<<endl;
               return;
           }
       }
   }
   for(ll i=0;i<n;i++)
   {
       if(b[i]==0)
       {
           auto it=s2.upper_bound(a[i]);
           it--;
           if((*it)<=a[i])
           {
               b[i]=*it;
               s2.erase(b[i]);
           }
           else
           {
               cout<<"NO"<<endl;
               return;
           }
       }
   }
   cout<<"YES"<<endl;
    for(auto x:a)
    cout<<x<<" ";
    cout<<endl;
    for(auto x:b)
    cout<<x<<" ";
    cout<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL) ;
    ll t=1; cin>>t;
    while(t--)
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points